<?php
    include($_SERVER['DOCUMENT_ROOT']."/ObstacleWebApp/view/header.php");
?>

<section>
<hgroup>
<h1>Screenings</h1>
</hgroup>

    <script>
    $(function() {
        $("#search").autocomplete({
        source: "zoekscreening.php",
        minLength: 2
        });
    });
    </script>

    <br>
    <form  name="registerform" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <?php
        if (isset($_GET['search']))
        { ?>
            <input placeholder="Achternaam" name="search" id="search" value="<?php echo $_GET['search']; ?>" />
        <?php }
        else
        { ?>
            <input placeholder="Achternaam" name="search" id="search" />
    <?php    }
    ?>
    
    
    <input type="submit" name="Submit" value="Zoeken">
</form>
    <br>
    
<?php
if (isset($_GET['start']))
{
    $start=$_GET['start'];
}
else
{
    $start=0;
}

if(strlen($start) > 0 and !is_numeric($start)){
	exit;
}
$eu = ($start - 0);
$limit = 20; // No of records to be shown per page.
$thi = $eu + $limit;
$back = $eu - $limit;
$next = $eu + $limit;

if (isset($_GET['search']))
{
    $page_name="screenings.php?search=".$_GET['search']."&";
    if ($_GET['search'] == "")
    {
        if ($_SESSION['admin'] == "T")
        {
            $query2="SELECT * from screeningversion ORDER BY Version DESC, Id DESC";
        }
        else
        {
            $query2="SELECT * from screeningversion WHERE UserId=".$_SESSION['userid']." ORDER BY Version DESC, Id DESC";
        }
    }
    else
    {
        if ($_SESSION['admin'] == "T")
        {
            $query2="SELECT * from screeningversion WHERE ScreeningId in(SELECT Id FROM screening WHERE PatientId IN(SELECT Id from patient WHERE LastName='".$_GET['search']."')) ORDER BY Version DESC, Id DESC";
        }
        else
        {
            $query2="SELECT * from screeningversion WHERE UserId=".$_SESSION['userid']." AND ScreeningId in(SELECT Id FROM screening WHERE PatientId IN(SELECT Id from patient WHERE LastName='".$_GET['search']."')) ORDER BY Version DESC, Id DESC";
        }
    }
}
else
{
    $page_name="screenings.php?";
    if ($_SESSION['admin'] == "T")
    {
        $query2="SELECT * from screeningversion ORDER BY Version DESC, Id DESC";
    }
    else
    {
        $query2="SELECT * from screeningversion WHERE UserId=".$_SESSION['userid']." ORDER BY Version DESC, Id DESC";
    }
}

$result2=mysql_query($query2);
$nume=mysql_num_rows($result2);
?>

<table id="rounded-corner">
    <thead>
    	<tr>
            <th scope="col" class="rounded-company">Bewoner</th>
            <th scope="col" class="rounded-q2">Versie</th>
            <th scope="col" class="rounded-q3">Bekijk/bewerk screening</th>
            <th scope="col" class="rounded-q4">Print screening</th>
        </tr>
    </thead>
        <tfoot>
    	<tr>
        	<td colspan="3" class="rounded-foot-left">
        	<?php
	        	if ( $limit < $nume) {
	        		if($back >=0) {
						echo "<a href='$page_name?start=$back'>Vorige</a>";
					}
	        		$i=0;
					$l=1;
					for($i=0;$i < $nume;$i=$i+$limit){
						if($i <> $eu)
						{
							echo " <a href='$page_name?start=$i'>$l</a> ";
						}
						else 
						{ 
							echo "<span style=\"font-weight:bold;\">$l</span>";
						}
						$l=$l+1;
					}
	        		if($thi < $nume) 
	        		{
						echo "<a href='$page_name?start=$next'>Volgende</a>";
					}
				}
        	?>
        	</td>
        	<td class="rounded-foot-right">&nbsp;</td>
        </tr>
    </tfoot>
    <tbody>
<?php

if (isset($_GET['search']))
{
    if ($_GET['search'] == "")
    {
        if ($_SESSION['admin'] == "T")
        {
            $sql1="SELECT * from screeningversion ORDER BY Version DESC, Id DESC limit ".$eu.",". $limit."";
        }
        else
        {
            $sql1="SELECT * from screeningversion WHERE UserId=".$_SESSION['userid']." ORDER BY Version DESC, Id DESC limit ".$eu.",". $limit."";
        }
    }
    else
    {
        if ($_SESSION['admin'] == "T")
        {
            $sql1="SELECT * from screeningversion WHERE ScreeningId in(SELECT Id FROM screening WHERE PatientId IN(SELECT Id from patient WHERE LastName='".$_GET['search']."')) ORDER BY Version DESC, Id DESC limit ".$eu.",". $limit."";
        }
        else
        {
            $sql1="SELECT * from screeningversion WHERE UserId=".$_SESSION['userid']." AND ScreeningId in(SELECT Id FROM screening WHERE PatientId IN(SELECT Id from patient WHERE LastName='".$_GET['search']."')) ORDER BY Version DESC, Id DESC limit ".$eu.",". $limit."";
        }
    }
}
else
{
    if ($_SESSION['admin'] == "T")
    {
        $sql1="SELECT * from screeningversion ORDER BY Version DESC, Id DESC limit ".$eu.",". $limit."";
    }
    else
    {
        $sql1="SELECT * from screeningversion WHERE UserId=".$_SESSION['userid']." ORDER BY Version DESC, Id DESC limit ".$eu.",". $limit."";
    }
}

$result1 = mysql_query($sql1) or die (mysql_error());
while($row = mysql_fetch_array($result1))
{
$query2 = "SELECT * FROM screening WHERE Id=".$row['Id']."";
$result2 = mysql_query($query2) or die (mysql_error());
while ($row2 = mysql_fetch_array($result2))
{
    $query3 = "SELECT * FROM patient WHERE Id=".$row2['PatientId']."";
    $result3 = mysql_query($query3) or die (mysql_error());
    while ($row3 = mysql_fetch_array($result3))
    {
        echo "<tr><td>".$row3['FirstName']." ".$row3['LastName']."</td>";
    }
}
echo "<td>Versie ".$row['Version']."</td>";
echo "<td><a href=\"screening.php?id=".$row['Id']."\" >Bekijk/bewerk screening</a></td>";
echo "<td><a href=\"screeningprint.php?id=".$row['Id']."\" >Print screening (PDF)</a></td></tr>";


}

?>
</tbody>
</table>


</section>

<?php
    include($_SERVER['DOCUMENT_ROOT']."/ObstacleWebApp/view/footer.php");
?>
